package xin.fsqr520.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.fsqr520.project.domain.po.PostComment;
import xin.fsqr520.project.mapper.PostCommentMapper;
import xin.fsqr520.project.mapper.PostMapper;
import xin.fsqr520.project.service.IPostCommentService;


@Service
public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment> implements IPostCommentService {


    @Autowired
    private PostCommentMapper postCommentMapper;

    @Autowired
    private PostMapper postMapper;

    @Override
    @Transactional
    public int addComment(Long userId, Long postId, String content, Long parentId) {
        //在postcomment表插入内容
        PostComment p = PostComment.builder()
                .postId(postId)
                .userId(userId)
                .parentId(parentId)
                .commentContent(content)
                .build();
        postCommentMapper.insert(p);

        //在post表修改评论数
        Integer i = postMapper.addComment(postId, 1);
        if (i == 1) return 1;
        return 0;
    }
}
